package com.wrupple.muba.catalogs.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayNumber;
import com.google.gwt.core.client.JsArrayString;
import com.wrupple.muba.common.client.JSOHelper;
import com.wrupple.muba.common.domain.FilterCriteria;
import com.wrupple.muba.common.domain.FilterData;
import com.wrupple.vegetate.domain.CatalogObject;
import com.wrupple.vegetate.domain.VegetateJSO;

@SuppressWarnings("serial")
public final class CatalogEntry extends VegetateJSO implements CatalogObject {

	protected CatalogEntry() {
	}

	public final native String getCatalogIdentity()/*-{
		var key = this.catalog;
		if (this.catalog_id_field == null) {
			this.catalog_id_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::idField(Ljava/lang/String;)(key);
			this.catalog_name_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::nameField(Ljava/lang/String;)(key);
		}

		if (this.catalog_identity == null) {
			this.catalog_identity = this[this.catalog_id_field] + "." + key;
		}
		//outcome may look something like "236780.Services@something.com"
		return this.catalog_identity;
	}-*/;

	@Override
	public final native void setIdAsString(String id) /*-{
		var key = this.catalog;
		if (this.catalog_id_field == null) {
			this.catalog_id_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::idField(Ljava/lang/String;)(key);
			this.catalog_name_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::nameField(Ljava/lang/String;)(key);
		}
		this[this.catalog_id_field] = id;
	}-*/;

	@Override
	public final native void setName(String name) /*-{
		var key = this.catalog;
		if (this.catalog_id_field == null) {
			this.catalog_id_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::idField(Ljava/lang/String;)(key);
			this.catalog_name_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::nameField(Ljava/lang/String;)(key);
		}
		this[this.catalog_name_field] = name;
	}-*/;

	@Override
	public final native String getIdAsString() /*-{
		var key = this.catalog;
		if (this.catalog_id_field == null) {
			try {
				this.catalog_id_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::idField(Ljava/lang/String;)(key);
				this.catalog_name_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::nameField(Ljava/lang/String;)(key);
				if(this.catalog_id_field==null){
					this.catalog_id_field="id";
				}
				if(this.catalog_name_field==null){
					this.catalog_name_field="name";
				}
			} catch (e) {
				return null;
			}
		}
		return this[this.catalog_id_field] + "";
	}-*/;

	@Override
	public final native String getName() /*-{
		var key = this.catalog;
		if (this.catalog_id_field == null) {
			try {
				this.catalog_id_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::idField(Ljava/lang/String;)(key);
				this.catalog_name_field = @com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogDescriptionServiceImpl::nameField(Ljava/lang/String;)(key);
				if(this.catalog_id_field==null){
					this.catalog_id_field="id";
				}
				if(this.catalog_name_field==null){
					this.catalog_name_field="name";
				}
			} catch (e) {
				return null;
			}
		}
		return this[this.catalog_name_field]+"";
	}-*/;

	public final native String getCatalog() /*-{
		return this.catalog;
	}-*/;
	
	public final native String getFieldNamedIdValue() /*-{
		if(this.id==undefined || this.id==null){
			return null;
		}else{
			return this.id;
		}
	}-*/;
	
	public  final native  String getFieldNamedName()  /*-{
		if(this.name==undefined || this.name==null){
			return null;
		}else{
			return this.name;
		}
	}-*/;
	
	public   final native  String getFieldNamedImage() /*-{
		if(this.image==undefined || this.image==null){
			return null;
		}else{
			return this.image;
		}
	}-*/;

	public static CatalogEntry createCatalogEntry(String catalog) {
		CatalogEntry entry = createObject().cast();
		setCatalog(entry, catalog);
		return entry;
	}

	public static final native void setCatalog(JavaScriptObject entry, String id) /*-{
		entry.catalog = id;
	}-*/;
	
	


	public static List<Long> convertLongArray(JsArrayNumber arr) {
		if (arr == null) {
			return null;
		}
		ArrayList<Long> regreso = new ArrayList<Long>(arr.length());
		long number;
		for (int i = 0; i < arr.length(); i++) {
			number = (long) arr.get(i);
			regreso.add(number);
		}
		return regreso;
	}

	public static JsArrayNumber convertLongArray(List<Long> list) {
		if (list == null) {
			return null;
		}
		JsArrayNumber regreso = JsArrayNumber.createArray().cast();
		int index = 0;
		for (Long l : list) {
			regreso.set(index, l);
			index++;
		}
		return regreso;
	}
	
	public static List<String> convertStringArray(JsArrayString arr) {
		if (arr == null) {
			return null;
		}
		ArrayList<String> regreso = new ArrayList<String>(arr.length());
		String number;
		for (int i = 0; i < arr.length(); i++) {
			number = arr.get(i);
			regreso.add(number);
		}
		return regreso;
	}

	public static Long parseKeyField(JavaScriptObject object, String string) {
		String name = JSOHelper.getAttribute(object, string);

		return name == null ? null : Long.parseLong(name);
	}

	public static FilterData filterByIds(String idField,List<String> ids) {
		FilterData filter = new FilterData();
		Map<String, List<FilterCriteria>> filters = new HashMap<String, List<FilterCriteria>>(1);
		List<FilterCriteria> arg1 = new ArrayList<FilterCriteria>(1);
		
		FilterCriteria criteria;
		criteria = new FilterCriteria();
		criteria.setOperator(FilterData.EQUALS);
		criteria.setValues(ids);
		arg1.add(criteria);
		
		filters.put(idField, arg1 );
		filter.setConstrained(false);
		filter.setFilters(filters);
		return filter;
	}

	public static FilterData filterByIds(String idField,
			JsArrayString childrenIds) {
		List<String> list  = convertStringArray(childrenIds);
		return filterByIds(idField, list);
	}

}
